//
//  SBMyWebViewController.m
//  SharingBikes
//
//  Created by 李经纬 on 2017/3/28.
//  Copyright © 2017年 李经纬. All rights reserved.
//

#import "SBMyWebViewController.h"

#import "SBWebProgressLayer.h"
#import <JavaScriptCore/JavaScriptCore.h>


@interface SBMyWebViewController ()<UIWebViewDelegate>
{
    SBWebProgressLayer *_progressLayer; ///< 网页加载进度条
    BOOL isFinish;
}
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UITextField *conversionCodeTextField;
@property (weak, nonatomic) IBOutlet UIView *conversionCodeView;

@end

@implementation SBMyWebViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self cleanCacheAndCookie];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self cleanCacheAndCookie];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    isFinish = NO;
}

/**清除缓存和cookie*/
- (void)cleanCacheAndCookie{
    
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
}

- (void)setupUI {

//    NSString *path = [[NSBundle mainBundle] pathForResource:@"meeting_test" ofType:@"html"];
//
//    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//
//    NSString *basePath = [[NSBundle mainBundle] bundlePath];
//
//    NSURL *baseURL = [NSURL fileURLWithPath:basePath];
//
//    [self.webView loadHTMLString:htmlString baseURL:baseURL];
    //发布方法二
//    NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
//    
//   NSString *path = [mainBundlePath stringByAppendingString:@"/Html/content/meeting"];
//    NSURL *baseUrl = [NSURL fileURLWithPath:path isDirectory:YES];
//    NSString *htmlPath = [NSString stringWithFormat:@"%@/index.html",path];
//    NSString *htmlString = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
//    [self.webView loadHTMLString:htmlString baseURL:baseUrl];
    //方法三
    NSURL * url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"Html/content/meeting"]];
    
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];
    
    
 
}


#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{


    return YES;
}




- (void)webViewDidStartLoad:(UIWebView *)webView {
    [_progressLayer startLoad];
    self.titleLabel.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    @WeakObj(self);
    
    [_progressLayer finishedLoad];
    self.titleLabel.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    isFinish = YES;
    
    
    
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //定义好JS要调用的方法, share就是调用的share方法名
    context[@"scan"] = ^() {
//        NSLog(@"+++++++Begin Log+++++++");
//        NSArray *args = [JSContext currentArguments];
    
//        
//        for (JSValue *jsVal in args) {
//            NSLog(@"%@", jsVal.toString);
//        }
        
    };
    
    context[@"clickBack"] = ^() { // 调用返回
//        NSArray *args = [JSContext currentArguments];
        dispatch_async(dispatch_get_main_queue(), ^{
            [selfWeak.navigationController popViewControllerAnimated:YES];
        });
        
    };
    context[@"clickOnLoginOut"] = ^() { // 退出登录
        dispatch_async(dispatch_get_main_queue(), ^{
           
            
            [selfWeak.navigationController popToRootViewControllerAnimated:YES];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get
//            });
            
        });
    };
    
 
    
}




- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [_progressLayer finishedLoad];
}

- (void)dealloc {
    
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [_progressLayer closeTimer];
    [_progressLayer removeFromSuperlayer];
    _progressLayer = nil;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- XibAction
- (IBAction)returnHomeViewController:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}



@end
